package com.easy.dfc.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.Utility;
import com.easy.core.web.BaseController;
import com.easy.dfc.Utils.Util;
import com.easy.dfc.entity.*;
import com.easy.dfc.service.FactoryHomeService;
import com.easy.dfc.service.UsersetService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

@Controller
@RequestMapping(value = "${apiPath}/factory")
public class FactoryHomeController extends BaseController {

    @Resource
    private FactoryHomeService factoryHomeService;

    @Resource
    private UsersetService usersetService;

    //@Resource
    //private SensitiveWordsMapper sensitiveWordsMapper;


    /**
     * 店铺首页
     * @param user
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "HomePage", method = RequestMethod.POST)
    public Result HomePage(@RequestBody UserPara user,Model model) {
        try {
            if (!usersetService.VerifyToken(user.getUserId(), user.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.HomePage(user);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 店铺全部宝贝
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AllProductList", method = RequestMethod.POST)
    public Result AllProductList(@RequestBody ParaAllProduct para, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.AllProductList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺新品上新Banner
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "NewProductBanner", method = RequestMethod.POST)
    public Result NewProductBanner(@RequestBody UserPara para, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.NewProductBanner(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 店铺新品上新产品
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "NewProductList", method = RequestMethod.POST)
    public Result NewProductList(@RequestBody UserPara para, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.NewProductList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 店铺视频点播&视频直播
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ViewVideo", method = RequestMethod.POST)
    public Result ViewVideo(@RequestBody UserPara para, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.ViewVideo(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺首页
     * @param user
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "Factory", method = RequestMethod.POST)
    public Result Factory(@RequestBody UserPara user,Model model) {
        try {
            if (!usersetService.VerifyToken(user.getUserId(), user.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.Factory(user);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 店铺升级
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "Upgrade", method = RequestMethod.POST)
    public Result Upgrade(@RequestBody ParaFactoryUpgrade para, HttpServletRequest request, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            StringBuffer URL = request.getRequestURL();
            String url = URL.substring(0,URL.length()-7);
            //StringBuffer URL = "http://"+request.getRemoteHost()+":"+request.getRemotePort()+"/"+request.getContextPath()+"/";
            List imageList = Util.UploadImagesList(request, "images");
            return factoryHomeService.Upgrade(para, imageList, url);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 店铺状态查询
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpgradeQuery", method = RequestMethod.POST)
    public Result UpgradeQuery(@RequestBody ParaFactoryUpgrade para, HttpServletRequest request, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return factoryHomeService.UpgradeQuery(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 升级支付
     * @param para
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpgradePay", method = RequestMethod.POST)
    public Result UpgradePay(@RequestBody ParaPayInfo para, HttpServletRequest request, Model model) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            StringBuffer URL = request.getRequestURL();
            String url = URL.substring(0,URL.length()-10);
            //StringBuffer URL = "http://"+request.getRemoteHost()+":"+request.getRemotePort()+"/"+request.getContextPath()+"/";

            return factoryHomeService.UpgradePay(para,url);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 支付完成回调
     * @param aliNotify
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "aliNotify")
    public String aliNotify(AliNotify aliNotify, HttpServletRequest request, Model model) {
        try {
            //boolean signVerified = SignUtils.rsaCheckV1(paramsMap, ALIPAY_PUBLIC_KEY, "UTF-8"); //调用SDK验证签名
            //if(signVerfied){
            //  验签成功后
            //按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure
            //}else{
            //  验签失败则记录异常日志，并在response中返回failure.
            //}
            Enumeration enu=request.getParameterNames();
            while(true){
                if (!(enu.hasMoreElements())) break;
                String paraName=(String)enu.nextElement();
                System.out.println(paraName+": "+request.getParameter(paraName));
                logger.error("ALIpay:"+ paraName+" : "+request.getParameter(paraName));
            }
            return factoryHomeService.aliNotify(aliNotify);
        } catch (Exception e) {
            logger.error(e.toString());
            return "fail";
        }
    }
}
